<? ini_set('memory_limit','10000M');?>
<style>
body { margin-top:25; font-size:12px;}
		.b_line { border-bottom:1px #AAAAAA solid; height:25px; font-size:14px}
		.border_t{border-top:1px inset #EEE;}
		.border_b{border-bottom:1px inset #EEE;}
		.border_l{border-left:1px inset #EEE;}
		.border_r{border-right:1px inset #EEE;}
		.xx:hover {cursor:pointer; background:#99FF99;}
		.xy:hover {cursor:pointer; background:#F30;}
		.box{border:1px solid #003}
		.box_t{border-top:1px solid #003;}
		.box_lbr{border:1px solid #003; border-top:none}
		.box_ltr{border:1px solid #003; border-bottom:none}
		.box_l{border-left:1px solid #003;}
		.box_r{border-right:1px solid #003;}
		.box_lb{border-left:1px solid #003;border-bottom:1px solid #003;}
		.box_rb{border-right:1px solid #003;border-bottom:1px solid #003;}
		.list_genap{cursor:pointer; background-color:#6699FF;}
		.list_ganjil{cursor:pointer; background-color:#9966FF;}
		.list_closed{cursor:pointer; background-color:#FF99FF;}
</style>
<table style='border-collapse:collapse' width='100%'>
	<tr align='center'><td colspan='4'><h2><?=$coy;?></h2><?=$add;?></td></tr>
	<tr align='center'><td colspan='4'><h1>BALANCE SHEET</h1></td></tr>
	<tr align=''><td colspan='4'><b>Period :<?=nBulan($bln). " ". $thn;?></b><hr></td></tr>
	<tr align='center' class="list_genap">
		<td width='50%'>Uraian</td>
		<td width='15%'>Periode Ini</td>
		<td width='15%'>Periode Lalu</td>
		<td width='15%'>Tahun Ini</td>
	</tr>
	<!--aktiva section-->
	<tr><td colspan='3'><font style="font-size:large">AKTIVA</font></td></tr>	
	<?
		switch($lvl)
		{
			case '2':$ln=5;break;
			case '3':$ln=9;break;
			case '4':$ln=13;break;
			case '5':$ln=13;break;
		}
	 $aktivane=0;$tot_ak=0;$rb=0;$kode='';$lvel="";
	 $tp_lalu =0;$tp_ini=0;$tp_ytd=0;
	 foreach($lst as $r){
		$aktv=0;$p_lalu=0;$p_ini=0;$p_ytd =0;
		$stp_lalu =0;$stp_ini=0;$stp_ytd=0;
		$p_lalu=($lvl!='2')?'0':$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,5)='".$r->ckode."' $where_lalu  group by left(koderek,5)");
		$p_ini =($lvl!='2')?'0':$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,5)='".$r->ckode."' $where_ini  group by left(koderek,5)");
		$p_ytd =($lvl!='2')?'0':$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,5)='".$r->ckode."' $where_ytd  group by left(koderek,5)");
		$stp_lalu +=$p_lalu;$stp_ini +=$p_ini;$stp_ytd +=$p_ytd;
		 ?>
		<tr>
			<td width='50%'><font color="#FFFFFF"><?=str_repeat("x",(strlen($r->ckode)-1));?></font><?=$r->susunan;?></td>
			<td width='15%' align='right'><?=number_format(((int)$p_ini),0);?></td>
			<td width='15%' align='right'><?=number_format(((int)$p_lalu),0);?></td>
			<td width='15%' align='right'><?=number_format(((int)$p_ytd),0);?></td>
		</tr>
		<?
		$ordby=empty($_POST['ordby'])?'ckode':$_POST['ordby'];
		$this->main_model->where(array('igrup'=>'0','levele <='=>$lvl,'levele >'=>'2'));
		$this->main_model->where_like('ckode',$r->ckode,'after');
		$datax=$this->main_model->generate_list('v_neraca',$ordby);
		foreach($datax as $d){
		?>
		<!--aktiva lancar-->
		<?
		$p_lalu=$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,".$ln.")='".$d->ckode."' $where_lalu  group by left(koderek,".$ln.")");
		$p_ini =$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,".$ln.")='".$d->ckode."' $where_ini  group by left(koderek,".$ln.")");
		$p_ytd =$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,".$ln.")='".$d->ckode."' $where_ytd  group by left(koderek,".$ln.")");
		$tp_lalu +=$p_lalu;$tp_ini +=$p_ini;$tp_ytd +=$p_ytd;
		$stp_lalu +=$p_lalu;$stp_ini +=$p_ini;$stp_ytd +=$p_ytd;
		?>
		<tr>
			<td width='50%'><font color="#FFFFFF"><?=str_repeat("x",(strlen($d->ckode)-1));?></font><?=$d->susunan;?></td>
			<td width='15%' align='right'><?=number_format(((int)$p_ini),0);?></td>
			<td width='15%' align='right'><?=number_format(((int)$p_lalu),0);?></td>
			<td width='15%' align='right'><?=number_format(((int)$p_ytd),0);?></td>
		</tr>
		<? }?>
		<tr>
			<td width='50%' align='right'>Jumlah Aktiva <?= (substr($r->ckode,0,5)=='1.001')?"Lancar":"Tetap";?>
			<font color="#FFFFFF"><?=str_repeat("x",15);?></font></td>
			<td width='15%' align='right'class='box_t'><?=number_format(((int)$stp_ini),0);?></td>
			<td width='15%' align='right'class='box_t'><?=number_format(((int)$stp_lalu),0);?></td>
			<td width='15%' align='right'class='box_t'><?=number_format(((int)$stp_ytd),0);?></td>
		</tr>
	<? }?>
		<tr>
			<td width='50%' align='right'>Jumlah Aktiva<font color="#FFFFFF"><?=str_repeat("x",15);?></font></td>
			<td width='15%' align='right'class='box_t'><?=number_format(((int)$tp_ini),0);?></td>
			<td width='15%' align='right'class='box_t'><?=number_format(((int)$tp_lalu),0);?></td>
			<td width='15%' align='right'class='box_t'><?=number_format(((int)$tp_ytd),0);?></td>
		</tr>
		
	<!--Pasiva section-->
	<tr><td colspan='3'>&nbsp;</td></tr>	
	<tr><td colspan='3'><font style="font-size:large">PASIVA</font></td></tr>	
	<?
	  $sbtkwj=0; $pasivax=0;
	  $ptp_lalu =0;$ptp_ini=0;$ptp_ytd=0;
	  foreach($lst2 as $r2){
		  ?>
		<!--<tr>
			<td width='50%'><font color="#FFFFFF"><?=str_repeat("x",(strlen($r2->ckode)+4));?></font><?=$r2->susunan;?></td>
			<td width='15%' align='right'>0</td>
			<td width='15%' align='right'></td>
		</tr>-->
		<?
		$kwjb=0;
		$aktv=0;$pp_lalu=0;$pp_ini=0;$pp_ytd =0;
		$pstp_lalu =0;$pstp_ini=0;$pstp_ytd=0;
		$ordby=empty($_POST['ordby'])?'ckode':$_POST['ordby'];
		$this->main_model->where_like('ckode',$r2->ckode,'after');
		$this->main_model->where(array('igrup'=>'1','levele <='=>$lvl,'levele >='=>'1'));
		$dataxx=$this->main_model->generate_list('v_neraca',$ordby);
		foreach($dataxx as $d2){
		$kwjb=$this->main_model->rdb('v_neraca','sum(jml) as jml',"where left(ckode,".($ln-4).")='".$d2->ckode."' group by left(ckode,".($ln-4).")");
		$sbtkwj +=$this->main_model->rdb('v_neraca','sum(jml) as jml',"where left(ckode,".($ln-4).")='".$d2->ckode."' group by left(ckode,".($ln-4).")");
		$pasivax=($pasivax+$kwjb);
		$pp_lalu=$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,".($ln-4).")='".$d2->ckode."' $where_lalu  group by left(koderek,".($ln-4).")");
		$pp_ini =$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,".($ln-4).")='".$d2->ckode."' $where_ini  group by left(koderek,".($ln-4).")");
		$pp_ytd =$this->main_model->rdb('v_jurnal','(sum(debet)-sum(kredit)) as saldo',"where left(koderek,".($ln-4).")='".$d2->ckode."' $where_ytd  group by left(koderek,".($ln-4).")");
		$ptp_lalu +=$pp_lalu;$ptp_ini +=$pp_ini;$ptp_ytd +=$pp_ytd;
		$pstp_lalu +=$pp_lalu;$pstp_ini +=$pp_ini;$pstp_ytd +=$pp_ytd;
		?>

		<!--kewajiban-->
		<tr>
			<td width='50%'><font color="#FFFFFF"><?=str_repeat("x",(strlen($d2->ckode)+4));?></font><?=$d2->susunan;?></td>
			<td width='15%' align='right'><?=number_format(((int)$pp_ini),0);?></td>
			<td width='15%' align='right'><?=number_format(((int)$pp_lalu),0);?></td>
			<td width='15%' align='right'><?=number_format(((int)$pp_ytd),0);?></td>
		</tr><? $kode=$d2->ckode; $lvel=$d2->levele;?>
		<? }
		if(substr($kode,0,5)=='2.001')
		{
		?>
		<tr>
			<td width='50%' align='right'>Jumlah Kewajiban<font color="#FFFFFF"><?=str_repeat("x",15);?></font></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$pstp_ini),0);?></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$pstp_lalu),0);?></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$pstp_ytd),0);?></td>
		</tr>
		<? 
		}else if(substr($kode,0,5)=='3.004')
		{
		$rb_ini=$this->main_model->rdb('jurnal','(sum(debet)-sum(kredit)) as jml',"where left(koderek,1) in ('4','5','6') $where_ini");
		$rb_lalu=$this->main_model->rdb('jurnal','(sum(debet)-sum(kredit)) as jml',"where left(koderek,1) in ('4','5','6') $where_lalu");
		$rb_ytd=$this->main_model->rdb('jurnal','(sum(debet)-sum(kredit)) as jml',"where left(koderek,1) in ('4','5','6') $where_ytd");
		?>
		<tr>
			<td width='50%' align=''><font color="#FFFFFF"><?=str_repeat("x",15);?></font>Laba / Rugi berjalan</td>
			<td width='15%' align='right' class=''><?=number_format(((int)$rb_ini),0);?></td>
			<td width='15%' align='right' class=''><?=number_format(((int)$rb_lalu),0);?></td>
			<td width='15%' align='right' class=''><?=number_format(((int)$rb_ytd),0);?></td>
		</tr>
		<tr>
			<td width='50%' align='right'>Jumlah Modal<font color="#FFFFFF"><?=str_repeat("x",15);?></font></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$pstp_ini+$rb_ini),0);?></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$pstp_lalu+$rb_lalu),0);?></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$pstp_ytd+$rb_ytd),0);?></td>
		</tr>
		<? 
		}
	}
	$pasiva=$this->main_model->rdb('v_neraca','sum(jml) as jml',"where left(ckode,1) in('2','3')/* n group by left(ckode,1)*/")
	?>
		<tr>
			<td width='50%' align='right'>Jumlah Pasiva<font color="#FFFFFF"><?=str_repeat("x",15);?></font></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$ptp_ini+$rb_ini),0);?></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$ptp_lalu+$rb_lalu),0);?></td>
			<td width='15%' align='right' class='box_t'><?=number_format(((int)$ptp_ytd+$rb_ytd),0);?></td>
		</tr>
		<!-----Footer tanda tangan ---->
			<? $kt=explode("-",$kota);?>
			<tr><td></td><td align='center'><?=$kt[0].", ". date('d F Y');?></td></tr>
			<tr><td height="50px">&nbsp;</td><td align='center' valign='top'><?='Keuangan,';?></td></tr>
			<tr><td></td><td align='center'><?=$keua;?></td></tr>
</table>	
